package com.design.patterns.list.factory;

import com.design.patterns.list.factory.absfactory.OrderPizzaOrCola;
import com.design.patterns.list.factory.absfactory.cola.bj.BJColaFactory;
import com.design.patterns.list.factory.absfactory.pizza.bj.BJPizzaFactory;
import com.design.patterns.list.factory.methodfactory.OrderPizzaFactory;
import com.design.patterns.list.factory.simplefactory.OrderPizza;
import com.design.patterns.list.factory.simplefactory.SimpleFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Wangzhiwen
 */
public class Customer {

    public static String getPizzaName() {
        System.out.println("input pizza name:");
        return getStr();
    }

    public static String getColaName() {
        System.out.println("input cola name:");
        return getStr();
    }

    public static String getPizzaRegion(){
        System.out.println("input pizza region:");
        return getStr();
    }

    public static String getStr(){
        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        try {
            return s.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 1.普通的实现方式，客户选择pizzaName通过OrderPizza内部的if语句来创建对象。
     */
    private static void commonMethods(){
        System.out.println("不使用工厂");
        String pizzaName = getPizzaName();
        com.design.patterns.list.factory.OrderPizza.print(pizzaName);
    }

    /**
     * 2.简单工厂模式
     */
    private static void simpleFactoryMethod(){
        String pizzaName = getPizzaName();
        // 方式一，直接使用工厂的静态方法
        System.out.println("Pizza静态工厂方法");
        OrderPizza.print(pizzaName);
        // 方式二，传入pizza工厂
        System.out.println("构造器传入Pizza工厂");
        OrderPizza orderPizza = new OrderPizza(new SimpleFactory());
        orderPizza.pizza(pizzaName);
        // 方式三，set方法传入pizza工厂
        System.out.println("Set方法传入Pizza工厂");
        OrderPizza pizza = new OrderPizza();
        pizza.setSimpleFactory(new SimpleFactory());
        pizza.pizza(pizzaName);
    }

    /**
     * 3.方法工厂模式
     */
    private static void methodFactory(){
        String pizzaRegion = getPizzaRegion();
        com.design.patterns.list.factory.methodfactory.OrderPizza orderPizza = OrderPizzaFactory.createOrderPizza(pizzaRegion);
        String pizzaName = getPizzaName();
        System.out.println("方法工厂模式，具体的实现交给子类");
        orderPizza.createPizza(pizzaName);
    }

    /**
     * 4.抽象工厂模式
     */
    private static void abstractFactory(){

        String pizzaName = getPizzaName();
        OrderPizzaOrCola orderPizzaOrCola = new OrderPizzaOrCola(new BJPizzaFactory());
        orderPizzaOrCola.orderPizza(pizzaName);
        String colaName = getColaName();
        orderPizzaOrCola.setFactory(new BJColaFactory());
        orderPizzaOrCola.orderCola(colaName);

    }

    public static void main(String[] args) {
        commonMethods();

        simpleFactoryMethod();

        methodFactory();

        abstractFactory();
    }

}
